var mysqlClient = require('../libs/mysqlClient');

function Service() {
    this.list = function (params, callback) {
        var sql = mysqlClient.buildQuerySql('t_user', params, true);
        mysqlClient.queryPage(sql, null, params.pageNumber, params.pageSize, callback);
    };
    this.retrieve = function (id, params, callback) {
        mysqlClient.getObject('t_user', {id: id}, callback);
    };
    this.create = function (params, callback) {
        mysqlClient.addObject('t_user', params, function (newUser) {
            if (params['score'] && params['score'] > 0) {
                mysqlClient.getObject('t_organization', {id: params['group']}, function (data) {
                    if (data) {
                        mysqlClient.updateObject('t_organization', {score: data['score'] - params['score']}, {id: params['group']}, function () {
                            mysqlClient.addObject('t_score', {
                                type: 2,
                                count: params['score'],
                                purpose: '新增用户，账户转入',
                                from_user: params['group'],
                                to_user: newUser.insertId
                            }, callback)
                        });
                    } else {
                        callback({error: '没有数据'});
                    }
                });
            }
        });

    };
    this.update = function (id, params, callback) {
        mysqlClient.getObject('t_user', {id: id}, function (user) {
            if (user) {
                mysqlClient.updateObject('t_user', params, {id: id}, function () {
                    if (params['score'] && params['score'] > 0) {
                        mysqlClient.getObject('t_organization', {id: user['group']}, function (data) {
                            if (data) {
                                const added = (params['score'] - user['score']);
                                mysqlClient.updateObject('t_organization', {score: data['score'] - added}, {id: user['group']}, function () {
                                    // console.log(params);
                                    let type = 1;
                                    if (added > 0) type = 2;
                                    mysqlClient.addObject('t_score', {
                                        type: type,
                                        count: Math.abs(added),
                                        purpose: '成员积分管理',
                                        from_user: user['group'],
                                        to_user: id
                                    }, callback)
                                });
                            } else {
                                callback({error: '没有数据'});
                            }
                        });
                    }
                });
            } else {
                callback({error: '没有数据'});
            }
        });
    }
    this.delete = function (id, params, callback) {
        mysqlClient.getObject('t_user', {id: id}, function (user) {
            mysqlClient.getObject('t_organization', {id: user['group']}, function (data) {
                if (data) {
                    mysqlClient.updateObject('t_organization', {score: data['score'] + user['score']}, {id: user['group']}, function () {
                        mysqlClient.deleteObject('t_user', {id: id}, function () {
                            mysqlClient.addObject('t_score', {
                                type: 1,
                                count: user['score'],
                                purpose: '成员积分回收',
                                from_user: params['*session']['id'],
                                to_user: user.id
                            }, callback)
                        });
                    });
                } else {
                    callback({error: '没有数据'});
                }
            });

        });
    }
}

exports.service = Service;